// $Id$
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Globalization;
using Salamanca.DataAccess;
using Salamanca.DataAccess.Collections;
using Salamanca.DataAccess.Data;

namespace Northwind.DomainModel.Data
{

    /// <summary />
    public partial class ShipperMapper
    {

        /// <summary />
        static ShipperMapper()
        {
            Resources.Sql.Culture=CultureInfo.InvariantCulture;
        }

        /// <summary />
        private DomainModelKeyedCollection<Shipper> _FindAll(DataMapperCollection dataMappers)
        {
            IDbCommand[] commands=new IDbCommand[1];

            commands[0]=Connection.CreateCommand();
            commands[0].CommandType=CommandType.Text;
            commands[0].CommandText=Resources.Sql.ShipperSelectAll;

            return Load(commands, dataMappers);
        }

        private Shipper.DataTransfer _GetDataTransferObject(IList<IDataRecord> records)
        {
            Shipper.DataTransfer data=new Shipper.DataTransfer();

            return FillData(data, records[0]);
        }

        /// <summary />
        internal static Shipper.DataTransfer FillData(Shipper.DataTransfer data, IDataRecord record)
        {
            int ord=record.GetOrdinal("ShipperID");
            if (!record.IsDBNull(ord))
                data.ShipperID=record.GetInt32(ord);

            ord=record.GetOrdinal("CompanyName");
            if (!record.IsDBNull(ord))
                data.CompanyName=record.GetString(ord);

            ord=record.GetOrdinal("Phone");
            if (!record.IsDBNull(ord))
                data.Phone=record.GetString(ord);

            return data;
        }

        /// <summary />
        private static IDbCommand _CreateSelectCommand(IDbConnection connection, ParameterBuilder parameterBuilder, Shipper.PrimaryKey key)
        {
            IDbCommand command=connection.CreateCommand();
            command.CommandType=CommandType.Text;
            command.CommandText=string.Format(
                CultureInfo.InvariantCulture,
                Resources.Sql.ShipperSelect,
                parameterBuilder.GetMarker("ShipperID", 1)
            );

            IDbDataParameter p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("ShipperID", 1);
            p.DbType=DbType.Int32;
            p.Value=key.Value;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            return command;
        }

        /// <summary />
        private static IDbCommand _CreateDeleteCommand(IDbConnection connection, ParameterBuilder parameterBuilder, Shipper.DataTransfer data)
        {
            IDbCommand command=connection.CreateCommand();
            command.CommandType=CommandType.Text;
            command.CommandText=string.Format(
                CultureInfo.InvariantCulture,
                Resources.Sql.ShipperDelete,
                parameterBuilder.GetMarker("ShipperID", 1)
            );

            IDbDataParameter p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("ShipperID", 1);
            p.DbType=DbType.Int32;
            p.Value=data.ShipperID;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            return command;
        }

        /// <summary />
        private static IDbCommand _CreateInsertCommand(IDbConnection connection, ParameterBuilder parameterBuilder, Shipper.DataTransfer data)
        {
            IDbCommand command=connection.CreateCommand();
            command.CommandType=CommandType.Text;
            command.CommandText=string.Format(
                CultureInfo.InvariantCulture,
                Resources.Sql.ShipperInsert,
                parameterBuilder.GetMarker("ShipperID", 1),
                parameterBuilder.GetMarker("CompanyName", 2),
                parameterBuilder.GetMarker("Phone", 3)
            );

            IDbDataParameter p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("ShipperID", 1);
            p.DbType=DbType.Int32;
            p.Value=data.ShipperID;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("CompanyName", 2);
            p.DbType=DbType.String;
            p.Value=data.CompanyName;
            p.Size=40;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("Phone", 3);
            p.DbType=DbType.String;
            p.Value=(object)data.Phone ?? (object)DBNull.Value;
            p.Size=24;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            return command;
        }

        /// <summary />
        private static IDbCommand _CreateUpdateCommand(IDbConnection connection, ParameterBuilder parameterBuilder, Shipper.DataTransfer data)
        {
            IDbCommand command=connection.CreateCommand();
            command.CommandType=CommandType.Text;
            command.CommandText=string.Format(
                CultureInfo.InvariantCulture,
                Resources.Sql.ShipperUpdate,
                parameterBuilder.GetMarker("CompanyName", 1),
                parameterBuilder.GetMarker("Phone", 2),
                parameterBuilder.GetMarker("ShipperID", 3)
            );

            IDbDataParameter p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("CompanyName", 1);
            p.DbType=DbType.String;
            p.Value=data.CompanyName;
            p.Size=40;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("Phone", 2);
            p.DbType=DbType.String;
            p.Value=(object)data.Phone ?? (object)DBNull.Value;
            p.Size=24;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("ShipperID", 3);
            p.DbType=DbType.Int32;
            p.Value=data.ShipperID;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            return command;
        }

        /// <summary>Gets the parameter builder instance.</summary>
        /// <value>The parameter builder instance.</value>
        protected ParameterBuilder ParameterBuilder
        {
            get
            {
                return _ParameterBuilder;
            }
        }

        private ParameterBuilder _ParameterBuilder;

    }

}
